package Chapter4;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Collection;

import static org.junit.Assert.*;

/**
 * Created by m.zabarylo on 9/26/2014.
 */
@RunWith(value = Parameterized.class)
public class DbTestData {

    private static WebDriver driver;
    private static StringBuffer verificationErrors = new StringBuffer();

    private String height;
    private String weight;
    private String bmi;
    private String bmiCategory;

    @Parameterized.Parameters
    public static Collection testData() throws Exception
    {
        return getTestData("D:\\ZabaryloMaksym\\Documentation\\Selenium Testing Tools Cookbook\\Chapter 4\\Test Data Files\\BmiTesting.mdb","SELECT Height, Weight, Bmi, Category FROM TestData");
    }

    public DbTestData(String height, String weight, String bmi, String bmiCategory)
    {
        this.height = height;
        this.weight = weight;
        this.bmi = bmi;
        this.bmiCategory = bmiCategory;
    }

    public static Collection<String[]> getTestData(String mdbFile, String sqlQuery) throws Exception
    {
        ArrayList<String[]> records = new ArrayList<String[]>();

        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String myDB = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + mdbFile;
        Connection connection = DriverManager.getConnection(myDB, "", "");

        java.sql.Statement statement = null;
        ResultSet resultSet = null;

        statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

        resultSet = statement.executeQuery(sqlQuery);
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();

        int cols =  resultSetMetaData.getColumnCount();

        while (resultSet.next())
        {

            String fields[] = new String[cols];
            int col = 0;
            for (int colIdx=1;colIdx<=cols;colIdx++)
            {
                fields[col] = resultSet.getString(colIdx);
                col++;
            }
            records.add(fields);
        }

        resultSet.close();
        statement.close();
        connection.close();

        return records;

    }

    @BeforeClass
    public static void setUp() throws Exception
    {
        driver = new FirefoxDriver();
        driver.get("http://dl.dropbox.com/u/55228056/bmicalculator.html");
    }

    @Test
    public void testBMICalculator()
    {
        try {
            WebElement heightField = driver.findElement(By.name("heightCMS"));
            heightField.clear();
            heightField.sendKeys(this.height);

            WebElement weightField = driver.findElement(By.name("weightKg"));
            weightField.clear();
            weightField.sendKeys(this.weight);

            WebElement calculateButton = driver.findElement(By.id("Calculate"));
            calculateButton.click();

            WebElement bmiLbael = driver.findElement(By.name("bmi"));
            assertEquals(this.bmi, bmiLbael.getAttribute("value"));

            WebElement bmiCategoryLabel = driver.findElement(By.name("bmi_category"));
            assertEquals(this.bmiCategory, bmiCategoryLabel.getAttribute("value"));

        } catch (Error e )
        {
            verificationErrors.append(toString());
        }
    }

    @AfterClass
    public static void tearDown() throws Exception
    {
        driver.quit();

        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString))
            fail(verificationErrorString);
    }
}
